<!DOCTYPE html>
<html>
<head>
    <title>Infinite Resistor Network</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        canvas {
            border: 1px solid #ccc;
            background-color: #fff;
        }
    </style>
</head>
<body>
    <canvas id="physicsCanvas" width="800" height="500"></canvas>
    <script>
        const canvas = document.getElementById('physicsCanvas');
        const ctx = canvas.getContext('2d');

        // Parameters for the drawing
        const startX = 150;
        const startY = 150;
        const s = 120; // Side length of the square
        const lw = 2; // Line width

        ctx.lineWidth = lw;
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';

        // --- 1. Draw the long horizontal dashed lines ---
        ctx.beginPath();
        ctx.setLineDash([8, 6]);
        // Top dashed line
        ctx.moveTo(startX - 100, startY);
        ctx.lineTo(startX + 3 * s + 100, startY);
        // Middle dashed line
        ctx.moveTo(startX - 100, startY + s / 2);
        ctx.lineTo(startX + 3 * s + 100, startY + s / 2);
        // Bottom dashed line
        ctx.moveTo(startX - 100, startY + s);
        ctx.lineTo(startX + 3 * s + 100, startY + s);
        ctx.stroke();

        // --- 2. Draw the dashed diagonal resistors ---
        ctx.beginPath();
        // First cell diagonals
        ctx.moveTo(startX, startY);
        ctx.lineTo(startX + s, startY + s / 2);
        ctx.moveTo(startX, startY + s);
        ctx.lineTo(startX + s, startY + s / 2);
        // Second cell diagonals
        ctx.moveTo(startX + s, startY);
        ctx.lineTo(startX + 2 * s, startY + s / 2);
        ctx.moveTo(startX + s, startY + s);
        ctx.lineTo(startX + 2 * s, startY + s / 2);
        ctx.stroke();

        // --- 3. Draw the solid frame structure ---
        ctx.beginPath();
        ctx.setLineDash([]); // Set to solid line
        // Top horizontal rail
        ctx.moveTo(startX, startY);
        ctx.lineTo(startX + 2 * s, startY);
        // Bottom horizontal rail
        ctx.moveTo(startX, startY + s);
        ctx.lineTo(startX + 2 * s, startY + s);
        // Vertical lines
        ctx.moveTo(startX, startY);
        ctx.lineTo(startX, startY + s);
        ctx.moveTo(startX + s, startY);
        ctx.lineTo(startX + s, startY + s);
        ctx.moveTo(startX + 2 * s, startY);
        ctx.lineTo(startX + 2 * s, startY + s);
        // Triangle at the end
        ctx.moveTo(startX + 2 * s, startY);
        ctx.lineTo(startX + 3 * s, startY + s / 2);
        ctx.lineTo(startX + 2 * s, startY + s);
        ctx.stroke();

        // --- 4. Add labels and text ---
        // Labels A and B
        ctx.font = 'italic 24px Times New Roman';
        ctx.textAlign = 'center';
        ctx.fillText('B', startX + s, startY - 15);
        ctx.fillText('A', startX + s, startY + s + 30);
        
        // Caption text
        ctx.font = '24px KaiTi'; // A common font for Chinese characters
        ctx.textAlign = 'center';
        ctx.fillText('电图 4.12.1', canvas.width / 2, startY + s + 80);

    </script>
</body>
</html>